package com.mtsmda.jpa.eclipseLink.vogella.run;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.mtsmda.jpa.eclipseLink.vogella.domain.ToDo;

public class ToDoRun {

	public static void main(String[] args) {
		EntityManager entityManager = Persistence
				.createEntityManagerFactory("MTSMDA_EL").createEntityManager();
		entityManager.getTransaction().begin();
		Query queryD = entityManager.createQuery("delete from ToDo td");
		int executeUpdate = queryD.executeUpdate();
		System.out.println("executeUpdate - " + executeUpdate);
		
		ToDo toDo1 = new ToDo("Drink tea", "I will drink tea in 8-00");
		ToDo toDo2 = new ToDo("Call on skype", "I will call on skype");
		
		entityManager.persist(toDo1);
		entityManager.persist(toDo2);
		
		entityManager.getTransaction().commit();
		
		System.out.println("\n*********\n");
		Query query = entityManager.createQuery("select td from ToDo td");
		List<ToDo> resultList = query.getResultList();
		for(ToDo toDo : resultList){
			System.out.println(toDo);
		}
		System.out.println("\n*********\n");
		
		entityManager.close();
	}

}